import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginInterface{
	//ȫ�ֱ������ڴ洢�����û�id
	public static String onlineID;
	
	private JFrame frame;
	private JButton signIn, login;
	private JLabel welcomeLabel, idJL, pwJL;
	private JTextField idJT;
	private JPasswordField pass;
	private JPanel jp1, jp2, jp3, jp4;

	public LoginInterface(){
		signIn = new JButton("ע��");
		signIn.addActionListener(new Lis());
		signIn.setSize(new Dimension(100, 30));
		login = new JButton("��½");
		login.addActionListener(new Lis());
		login.setSize(new Dimension(100, 30));
		welcomeLabel = new JLabel("��ӭ��¼���Ѳ�ϵͳ");
		idJL = new JLabel("�˺ţ�");
		pwJL = new JLabel("���룺");
		idJT = new JTextField();
		idJT.setPreferredSize(new Dimension(300, 30));
		pass = new JPasswordField();
		pass.setPreferredSize(new Dimension(300, 30));
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		frame = new JFrame("���Ѳ�");
	}
	
	public void launch(){
		frame.setSize(400, 250);
		frame.setLayout(new GridLayout(4, 1));
		jp1.add(welcomeLabel);
		frame.add(jp1);
		jp2.add(idJL);
		jp2.add(idJT);
		frame.add(jp2);
		jp3.add(pwJL);
		jp3.add(pass);
		frame.add(jp3);
		jp4.add(signIn);
		jp4.add(login);
		frame.add(jp4);
		
		
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//�ڲ���Lis�����¼�����
	class Lis implements ActionListener {
		public void actionPerformed(ActionEvent e){
			Object source = e.getSource();
			if(source == signIn) {
				//����ע�ᴰ��
				frame.dispose();
				RegisterInterface myShow = new RegisterInterface();
				myShow.launch();
				
			}else if(source == login) {
				//��֤��½
				String id = idJT.getText();
				String pw = pass.getText();
				try {
					DatabaseCtrl.init();
					if(DatabaseCtrl.validateLogin(id, pw)) {
						onlineID = id;
						System.out.println("id = " + onlineID);
						DatabaseCtrl.close();
						
						frame.dispose();
						//ת��������
						MainMenu myMenu = new MainMenu();
						myMenu.launch();
						
					}else {
						DatabaseCtrl.close();
						//��ʾ�������
						JOptionPane.showMessageDialog(null, "��������˺Ż����벻��ȷ", "������ʾ!", JOptionPane.ERROR_MESSAGE);
					}
					DatabaseCtrl.close();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		}
	}
	
	public static void main(String[] args) {
		LoginInterface myShow = new LoginInterface();
		myShow.launch();
	}

}